package com.betty.cscams.controller;


import com.betty.cscams.constant.SysConstants;
import com.betty.cscams.entity.BusTerm;
import com.betty.cscams.service.BusTermService;
import com.betty.cscams.service.SysFileService;
import com.betty.cscams.vo.ScoreQueryBean;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.betty.cscams.common.http.HttpResult;
import com.betty.cscams.common.page.PageRequest;
import com.betty.cscams.service.BusSynthesisService;

import javax.servlet.http.HttpServletResponse;

/**
 * ---------------------------
 *  (BusSynthesisController)         
 * ---------------------------
 * 时间：  2022-03-14 16:41:44
 * ---------------------------
 */
@RestController
@RequestMapping("busSynthesis")
public class BusSynthesisController {

	@Autowired
	private BusSynthesisService busSynthesisService;
	@Autowired
	private BusTermService busTermService;
	@Autowired
	private SysFileService fileService;


    /**
     * 基础分页查询
     * @param pageRequest
     * @return
     */    
	@PostMapping(value="/findPage")
	public HttpResult findPage(@RequestBody PageRequest pageRequest) {
		return HttpResult.ok(busSynthesisService.findPage(pageRequest));
	}
	




	/**
	 * 导出
	 * @param params
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/exportSports")
	public void exportSports(@RequestBody ScoreQueryBean params, HttpServletResponse response) throws Exception {
		try{
			busSynthesisService.exportSport(params,response);
			//return HttpResult.ok("导出成功");
		}catch (Exception e){
			//return HttpResult.error("操作失败");
		}

	}

	/**
	 * 计算
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/calculationSports")
	public HttpResult calculationSports(@RequestBody ScoreQueryBean params) throws Exception {
		BusTerm busTerm = busTermService.findById(params.getTermId());
		if(ObjectUtils.isEmpty(busTerm) || !SysConstants.TERM_STATUS_2.equals(busTerm.getStatus())){
			return HttpResult.error("当前学期未开始");
		}
		try{
			busSynthesisService.calculationSports(params.getTermId(),params.getDeptId());
			return HttpResult.ok("操作成功");
		}catch (Exception e){
			return HttpResult.error("操作失败");
		}

	}

	/**
	 * 清空
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/resetSports")
	public HttpResult resetSports(@RequestBody ScoreQueryBean params) throws Exception {
		BusTerm busTerm = busTermService.findById(params.getTermId());
		if(ObjectUtils.isEmpty(busTerm) || !SysConstants.TERM_STATUS_2.equals(busTerm.getStatus())){
			return HttpResult.error("当前学期未开始");
		}
		try{
			busSynthesisService.calculationReset(params.getTermId() ,params.getDeptId());
			return HttpResult.ok("操作成功");
		}catch (Exception e){
			return HttpResult.error("操作失败");
		}

	}


}
